package models

import (
	"cloudops/src/common"
	"cloudops/src/config"
	"fmt"
	"time"
)

var (
	hostFormConfig = `{
  "schemas": [
    {
      "component": "Select",
      "label": "公有云选择",
      "field": "vendor",
      "colProps": {
        "span": 24
      },
      "componentProps": {
        "options": [
          {
            "label": "阿里云",
            "value": "ali"
          },
          {
            "label": "华为云",
            "value": "hw"
          },
          {
            "label": "aws",
            "value": "aws"
          }
        ],
        "size": null,
        "optionType": "button",
        "buttonStyle": "solid",
        "mode": null
      },
      "itemProps": {
        "labelCol": {
          "span": 0
        },
        "wrapperCol": {},
        "validateStatus": "success",
        "required": true
      }
    },
    {
      "component": "InputNumber",
      "label": "数量",
      "field": "num",
      "colProps": {
        "span": 24
      },
      "componentProps": {
        "style": "width:200px",
        "defaultValue": null,
        "size": "default",
        "max": null,
        "bordered": true,
        "min": null
      },
      "itemProps": {
        "labelCol": {},
        "wrapperCol": {},
        "required": false
      },
      "rules": [
        {
          "pattern": "",
          "message": ""
        },
        {
          "pattern": "",
          "message": ""
        },
        {
          "pattern": "",
          "message": ""
        }
      ]
    },
    {
      "component": "Select",
      "label": "实例规格",
      "field": "instance_type",
      "colProps": {
        "span": 24
      },
      "componentProps": {
        "options": [
          {
            "label": "4c8g",
            "value": "4c8g"
          },
          {
            "label": "8c16g",
            "value": "8c16g"
          },
          {
            "label": "16c32g",
            "value": "16c32g"
          },
          {
            "label": "64c500g",
            "value": "64c500g"
          }
        ],
        "multiple": true,
        "mode": "multiple"
      },
      "itemProps": {
        "labelCol": {},
        "wrapperCol": {},
        "required": true,
        "validateStatus": "success"
      }
    },
    {
      "component": "InputTextArea",
      "label": "主机名列表",
      "field": "hostnames",
      "colProps": {
        "span": 24
      },
      "componentProps": {
        "autocomplete": "on",
        "autocorrect": "on"
      },
      "itemProps": {
        "labelCol": {},
        "wrapperCol": {},
        "labelAlign": "right",
        "required": false
      }
    },
    {
      "component": "InputNumber",
      "label": "绑定的服务树叶子节点",
      "field": "bindLeafNodeId",
      "colProps": {
        "span": 24
      },
      "componentProps": {
        "style": "width:200px"
      },
      "itemProps": {
        "labelCol": {},
        "wrapperCol": {}
      }
    },
    {
      "component": "Switch",
      "label": "是否自动开机",
      "field": "open",
      "colProps": {
        "span": 24
      },
      "componentProps": {},
      "itemProps": {
        "labelCol": {},
        "wrapperCol": {}
      }
    },
    {
      "component": "StrengthMeter",
      "label": "开机密码",
      "colProps": {
        "span": 24
      },
      "field": "password",
      "componentProps": {},
      "itemProps": {
        "labelCol": {},
        "wrapperCol": {}
      }
    }
  ],
  "layout": "horizontal",
  "labelLayout": "flex",
  "labelWidth": 100,
  "labelCol": {},
  "wrapperCol": {},
  "colon": true
}`
)

func MockWorkOrder(sc *config.ServerConfig) {

	// 工单所需
	defineUserOrGroupsMap := map[string]string{
		"用户@admin":     "",
		"用户@test":      "",
		"组@super":      "",
		"组@frontAdmin": "",
	}

	//processes := []Process{}

	num := 5
	pServiceAccount := Process{
		Name:   sc.WorkOrderAutoActionC.AutoTemplateNameBuyEcs,
		UserID: 1,
		FlowNodes: []FlowNode{
			{
				Type:              common.FLOW_TYPE_Approval,
				DefineUserOrGroup: fmt.Sprintf("用户@%v", u1.Username),
			},
			{
				Type:              common.FLOW_TYPE_Approval,
				DefineUserOrGroup: fmt.Sprintf("用户@%v", u2.Username),
			},
			{
				Type:              common.FLOW_TYPE_Action,
				DefineUserOrGroup: fmt.Sprintf("用户@%v", u3.Username),
			},
		},
	}
	pServiceAccount.CreateProcess()

	dServiceAccount := &FormDesign{
		Name:       sc.WorkOrderAutoActionC.AutoTemplateNameBuyEcs,
		UserID:     1,
		FormConfig: hostFormConfig,
	}
	dServiceAccount.CreateOneFormDesign()
	for i := 1; i < num; i++ {
		randGroups := []string{}
		for name := range defineUserOrGroupsMap {
			randGroups = append(randGroups, name)
		}

		tmpNodes := []FlowNode{
			{
				Type:              common.FLOW_TYPE_Approval,
				DefineUserOrGroup: randGroups[0],
			},
			{
				Type:              common.FLOW_TYPE_Approval,
				DefineUserOrGroup: randGroups[1],
			},

			{
				Type:              common.FLOW_TYPE_Action,
				DefineUserOrGroup: randGroups[2],
			},
			{
				Type:              common.FLOW_TYPE_Action,
				DefineUserOrGroup: randGroups[3],
			},
		}
		p := Process{
			Name:      fmt.Sprintf("procees-%v", i),
			UserID:    1,
			FlowNodes: tmpNodes,
		}

		p.CreateProcess()

	}
	for i := 1; i < num; i++ {
		d := &FormDesign{
			Model:      Model{},
			Name:       fmt.Sprintf("FormDesign-%v", i),
			UserID:     uint(i%2) + 1,
			FormConfig: hostFormConfig,
		}
		d.CreateOneFormDesign()

	}

	tServiceAccount := WorkOrderTemplate{
		Name:         sc.WorkOrderAutoActionC.AutoTemplateNameBuyEcs,
		UserID:       1,
		FormDesignID: 1,
		ProcessID:    1,
	}
	tServiceAccount.CreateOneWorkOrderTemplate()

	for i := 1; i < num; i++ {
		t := WorkOrderTemplate{
			Model:        Model{},
			Name:         fmt.Sprintf("WorkOrderTemplate-%v", i),
			UserID:       uint(i%2) + 1,
			FormDesignID: uint(i),
			ProcessID:    uint(i),
		}
		t.CreateOneWorkOrderTemplate()
	}

	wServiceAccount := WorkOrderInstance{
		Model:             Model{},
		Name:              "创建虚拟机001",
		ActualApiJsonData: `{"instance_type":"8c16g","vendor":"ali","bindLeafNodeId":13,"hostnames":"host-1\nhost-2"}`,
		DesireFinishTime:  time.Now().Format("2006-01-02 15:04:05"),
		TemplateId:        1,
		UserID:            1,
	}
	wServiceAccount.CreateOneWorkOrderInstance()
	for i := 1; i < num; i++ {
		wi := WorkOrderInstance{
			Model:             Model{},
			Name:              fmt.Sprintf("工单-%v", i),
			ActualApiJsonData: `{"_checkbox_1":true,"_input_2":"www我的文档","_input_text_area_3":"我的文档"}`,
			DesireFinishTime:  time.Now().Format("2006-01-02 15:04:05"),
			TemplateId:        uint(i),
			UserID:            uint(i%2) + 1,
		}
		wi.CreateOneWorkOrderInstance()
	}
	num = 5

}
